package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.Seckill;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendGoodsMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.BackendSeckillMapper;
import com.burgeon.backend.SHOPBACKEND.service.BackendSeckillService;
import com.burgeon.backend.SHOPBACKEND.service.ex.DeleteException;
import com.burgeon.backend.SHOPBACKEND.service.ex.UploadException;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class BackendSeckillServiceImpl implements BackendSeckillService {

    @Autowired
    private BackendSeckillMapper backendSeckillMapper;
    @Autowired
    private BackendGoodsMapper backendGoodsMapper;

    @Override
    public void timingPutaway() {
        List<Seckill> data = backendSeckillMapper.findAll();
        Date date = new Date();
        SimpleDateFormat sdfe = new SimpleDateFormat("yyyy/MM/dd" + " " + "HH:mm:ss");

        for (Seckill seckill : data) {
            try {
                if (date.getTime() > sdfe.parse(seckill.getDateend()).getTime()) {
                    backendSeckillMapper.putaway(seckill.getId(), "N");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    @Transactional
    public void addSeckill(String code, String putaway,String price, String endTime, String account) throws UploadException {


        Date date = new Date();

        SimpleDateFormat sdfc = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat sdfe = new SimpleDateFormat("yyyy/MM/dd" + " " + "HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (date.getTime() > (sdfc.parse(endTime)).getTime()) {
                throw new UploadException("时间已过期");
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new UploadException("日期格式错误");
        }
        Seckill seckill = new Seckill();
        seckill.setAccount(account);
        seckill.setToken(account);
        seckill.setGid( backendGoodsMapper.getGidByCode(code,account));

        seckill.setPutaway(putaway);
        seckill.setPrice(price);
        try {
            seckill.setDateend(sdfe.format(sdfc.parse(endTime)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        seckill.setCreateTime(sdf.format(new Date()));
        backendSeckillMapper.addSeckill(seckill);
        backendGoodsMapper.upGoodsState(seckill.getGid(),"N");

    }

    @Override
    public List<Seckill> getData(String account) {
        List<Seckill> data = backendSeckillMapper.getData(account);
        for (Seckill seckill : data) {
            seckill.setGoods(backendGoodsMapper.getGoodsById(seckill.getGid()));
        }
        return data;
    }

    @Override
    public void delData(String id) throws DeleteException {
        try {
            backendSeckillMapper.delData(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DeleteException("出现错误");
        }
    }

    @Override
    public void putaway(String id, String yn) throws ErrorDataException {
        Date date = new Date();
        SimpleDateFormat sdfe = new SimpleDateFormat("yyyy/MM/dd" + " " + "HH:mm:ss");
        Seckill seckill = backendSeckillMapper.getDataById(id);

        try {
            if (date.getTime() > sdfe.parse(seckill.getDateend()).getTime()) {
                throw new ErrorDataException("时间已过期");
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new ErrorDataException("出现错误");
        }

        try {
            backendSeckillMapper.putaway(id, yn);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException("出现错误");
        }

    }


}
